import {parseFinder} from "echarts/src/util/model";

export default {
  install: function (Vue, options) {
    //实时更新storage值
    Vue.prototype.resetSetItem = function (key, newVal) {
      // 创建一个StorageEvent事件
      var newStorageEvent = document.createEvent('StorageEvent');
      const storage = {
        setItem: function (k, val) {
          localStorage.setItem(k, val);
          // 初始化创建的事件
          newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
          // 派发对象
          window.dispatchEvent(newStorageEvent)
        }
      }
      return storage.setItem(key, newVal);
    },
      //判断添加明细字段是否为空或为小于等于0
      Vue.prototype.checkEmy = function(elem){
        let isFieldNotEmy = true;
        for (let i=0; i<$('.' + elem + ':not(:hidden)').length; i++) {
          if ($('.' + elem + ':not(:hidden)').eq(i).val() <= 0 || $('.' + elem + ':not(:hidden)').eq(i).val() == '') {
            isFieldNotEmy = false;
            break;
          }
        }
        return isFieldNotEmy;
      },
      //判断添加明细字段是否为空或小于0
      Vue.prototype.checkEmy2 = function(elem){
        let isFieldNotEmy = true;
        for (let i=0; i<$('.' + elem).not(":hidden").length; i++) {
          if ($('.' + elem).not(":hidden").eq(i).val() < 0 || $('.' + elem).not(":hidden").eq(i).val() === '') {
            isFieldNotEmy = false;
            break;
          }
        }
        return isFieldNotEmy;
      },
      //判断添加明细字段是否为空
      Vue.prototype.checkEmy3 = function(elem){
        let isFieldNotEmy = true;
        for (let i=0; i<$('.' + elem).not(":hidden").length; i++) {
          if ($('.' + elem).not(":hidden").eq(i).val() === '') {
            isFieldNotEmy = false;
            break;
          }
        }
        return isFieldNotEmy;
      },
      //判断添加明细字段是否为空
      Vue.prototype.checkEmy4 = function(elem){
        let isFieldNotEmy = true;
        for (let i=0; i<$('.' + elem).length; i++) {
          if ($('.' + elem).eq(i).val() == '') {
            isFieldNotEmy = false;
            break;
          }
        }
        return isFieldNotEmy;
      },
      //判断添加明细字段是否为空或为零
      Vue.prototype.checkEmy5 = function(elem){
        let isFieldNotEmy = true;
        for (let i=0; i<$('.' + elem).length; i++) {
          if ($('.' + elem).eq(i).val() == '' || $('.' + elem).eq(i).val() == 0) {
            isFieldNotEmy = false;
            break;
          }
        }
        return isFieldNotEmy;
      },
      //判断input number是否不为空且大于0
      Vue.prototype.checkNumOk = function(num){
        return num != '' && parseFloat(num) > 0;
      },
      Vue.prototype.serialize2Obj = function (data) {
        var obj = {};
        $.each(data,function(i,v){
          obj[v.name] = v.value;
        })
        return obj;
      },
      //保留两位小数
      Vue.prototype.toDecimal2NoZero = function (x) {
        var f = Math.round(x * 100) / 100;
        var s = f.toString();
        return s;
      },
      Vue.prototype.toDecimal4NoZero = function (x) {
        var f = x.toFixed(4);
        var s = f;
        return s;
      },
      Vue.prototype.toFix2 = function(num) {
        return Math.round(num * 100) / 100;
      },
      Vue.prototype.toFix3 = function(num) {
        return Math.round(num * 1000) / 1000;
      },
      Vue.prototype.toFloorFix3 = function(num) {
        return Math.floor(num * 1000) / 1000;
      },
      Vue.prototype.toFix4 = function(num) {
        return Math.round(num * 10000) / 10000;
      },
      Vue.prototype.toFix6 = function(num) {
        return Math.round(num * 1000000) / 1000000;
      },
      //通用toast
      Vue.prototype.toastSuccess = function (text) {
        $.toast(text, 1000);
      },
      Vue.prototype.toastError = function (text) {
        $.toast.prototype.defaults.duration = 1000;
        $.toast(text, 'forbidden');
      },
      Vue.prototype.toastText = function (text) {
        $.toast.prototype.defaults.duration = 2000;
        $.toast(text, 'text');
      },
      Vue.prototype.toastText3 = function (text) {
        $.toast.prototype.defaults.duration = 3000;
        $.toast(text, 'text');
      },
      Vue.prototype.toastText5 = function (text) {
        $.toast.prototype.defaults.duration = 5000;
        $.toast(text, 'text');
      },
      Vue.prototype.checkNull = function (val) {
        return val != '' && val != undefined && val.length != '';
      },
      Vue.prototype.checkObjNull = function (val) {
        return val != undefined && val != null ? val : '';
      },
      //判断是否登录
      Vue.prototype.checkLogin = function() {
        this.axios.get('/system/Tuser!getWxuser.action')
          .then(response => {
            //前后端共同判断登录是否过期
            if (response.data.msg == 'false') {
              $.toast('会话已过期，请重新登录！', 1000);
              setTimeout(function () {
                location.href = '/weixin/WechatLogin!loginpage.action?openid=' + localStorage.openid;
              }, 1000)
            }
          });
      },
      //根据用户id和时间戳生成随机数
      Vue.prototype.createToken = function() {
        return localStorage.getItem('userid') + Date.now();
      },
      //返回上一页
      Vue.prototype.toPrevUrl = function(url) {
        let tempurl = url;
        if (!(tempurl.substring(0, 7) == 'http://' || tempurl.substring(0, 8) == 'https://')) {
          tempurl = decodeURIComponent(tempurl);
        }
        if (!(tempurl.substring(0, 7) == 'http://' || tempurl.substring(0, 8) == 'https://')) {
          tempurl = decodeURIComponent(tempurl);
        }
        if (!(tempurl.substring(0, 7) == 'http://' || tempurl.substring(0, 8) == 'https://')) {
          tempurl = decodeURIComponent(tempurl);
        }
        location.href = tempurl;
      },
      //判断是否是数组
      Vue.prototype.isArray = function(obj){
        if(typeof obj=="object" && obj.constructor==Array){
          return true;
        }
        return false;
      },
      //过滤null并加单位
      Vue.prototype.attachUnit = function(attr, unit){
        return attr ? attr + unit : '';
      },
      //过滤null并加单位,默认0
      Vue.prototype.attachUnit2 = function(attr, unit){
        return attr ? attr + unit : 0 + unit;
      },
      //表单值转换为数字
      Vue.prototype.toNum = function(value){
        return isNaN(parseFloat(value)) ? 0 : parseFloat(value);
      },
      Vue.prototype.obj2string = function(o){
        var r=[];
        if(typeof o=="string"){
          return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
        }
        if(typeof o=="object"){
          if(!o.sort){
            for(var i in o){
              r.push(i+":"+obj2string(o[i]));
            }
            if(!!document.all&&!/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)){
              r.push("toString:"+o.toString.toString());
            }
            r="{"+r.join()+"}";
          }else{
            for(var i=0;i<o.length;i++){
              r.push(obj2string(o[i]))
            }
            r="["+r.join()+"]";
          }
          return r;
        }
        return o.toString();
      },
    //版本号
      Vue.prototype.version = '1.0.8'
  }
}
